package com.bq.gametrade.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bq.gametrade.HttpLink;
import com.bq.gametrade.R;
import com.bq.gametrade.adapter.CancelledRecordedAdapter;
import com.bq.gametrade.base.BaseActivity;
import com.bq.gametrade.bean.CancelledRecorded;
import com.bq.gametrade.bean.CancelledRecordedDetails;
import com.bq.gametrade.bean.ResponseBean;
import com.bq.gametrade.bean.UserInfoDetails;
import com.bq.gametrade.http.VRHttp;
import com.bq.gametrade.http.VRHttpListener;
import com.bq.gametrade.widget.PromptAlertDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class CancelledRecordedActivity extends BaseActivity implements View.OnClickListener{

    private TextView titleName;
    private ImageView imgService,imgBack;
    private ListView listView;

    private ArrayList<CancelledRecordedDetails> mList = new ArrayList<>();
    private CancelledRecordedAdapter adapter ;

    private Activity activity;
    private LinearLayout layPrompt;
    private UserInfoDetails userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cancelledrecorded);
        if(application.isLogin()){
            userInfo = application.getUserInfo();
        }else {
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        }
       activity = this;
        initView();
    }


    @Override
    protected void onResume() {
        super.onResume();
        getCancelledRecord();
    }

    private void initView() {
        titleName = serchViewById(R.id.title_name);
        titleName.setText("挂单记录");
        imgService = serchViewById(R.id.iv_userService);
        imgService.setOnClickListener(this);
        imgBack = serchViewById(R.id.image_back);
        imgBack.setOnClickListener(this);
        listView = serchViewById(R.id.lv_cancelledRecorded);
        adapter = new CancelledRecordedAdapter(activity,mList);
        listView.setAdapter(adapter);
        adapter.setOnItemClickLitener(new CancelledRecordedAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(int position,final String cancelledId) {
                new PromptAlertDialog(activity).builder().setTitle("提示").setTouchOutsideCanceled(true)
                        .setMsg("您确定取消本次挂单？")
                        .setPositiveButton("确定", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                               removeCanncelled(cancelledId);
                            }
                        }).setNegativeButton("取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                }).show();
            }
        });

        layPrompt = serchViewById(R.id.lay_prompt);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_userService:

                break;
            case R.id.image_back:
                finish();
                break;

        }
    }

    /**
     * 获取挂单记录
     */
    private void getCancelledRecord(){
        Map<String, String> params = new HashMap<>();
        params.put("token",application.getUserInfo().token);
        showSpotsDialog();
        VRHttp.getCancelledRecordList(activity, HttpLink.CANCELLEDRECORED_URL, params, new VRHttpListener() {
            @Override
            public void onSuccess(Object response, boolean isCache) {
                if(response != null){
                    CancelledRecorded cancelledRecorded = (CancelledRecorded)response;
                    if(cancelledRecorded.status.equals("0")){
                        mList = cancelledRecorded.order_list;
                        if(mList.size()>0){
                            layPrompt.setVisibility(View.GONE);
                            adapter.setData(mList);
                            adapter.notifyDataSetChanged();
                        }else {
                            layPrompt.setVisibility(View.VISIBLE);
                        }
                    }else if(cancelledRecorded.status.equals("999")){
                        toastShow("登录超时，请重新登陆");
                        application.setUserInfo(null);
                        startActivity(new Intent(activity,LoginActivity.class));
                        finish();
                    }else if (cancelledRecorded.status.equals("98")) {
                        toastShow("您的账号已在其它设备登录，请重新登录修改您的密码");
                        application.setUserInfo(null);
                        startActivity(new Intent(activity, LoginActivity.class));
                        finish();
                    }

                }
            }

            @Override
            public void onError(String error) {
                super.onError(error);
                closeSpotsDialog();
                toastShow("获取数据失败");
            }

            @Override
            public void onFinish() {
                super.onFinish();
                closeSpotsDialog();
            }
        });

    }
    /**
     * 取消本次挂单
     */

    private void removeCanncelled(String cancelledId){
        Map<String, String> params = new HashMap<>();
        params.put("token",userInfo.token);
        params.put("orderId",cancelledId);
        showSpotsDialog("正在取消····");
        VRHttp.sendRequest(activity, HttpLink.REMOCE_CANCELLED_URL, params, new VRHttpListener() {
            @Override
            public void onSuccess(Object response, boolean isCache) {
                if (response != null){
                    ResponseBean responseBean = (ResponseBean) response;
                    if(responseBean.status.equals("0")){
                        toastShow("本次挂单取消成功！");
                        getCancelledRecord();
                    }
                }

            }

            @Override
            public void onError(String error) {
                super.onError(error);
                closeSpotsDialog();
                toastShow("取消挂单失败");
            }

            @Override
            public void onFinish() {
                super.onFinish();
                closeSpotsDialog();
            }
        });
    }
}
